package cn.biao.epic.sys.core.auth.entrypoint;

import cn.biao.epic.base.auth.exception.enums.AuthExceptionEnum;
import cn.biao.epic.model.response.ErrorResponseData;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @Auther Uncle_Z
 * @date 2019/12/16 14:49
 * @Email:2545375@qq.com
 * @Descrition
 */
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                         AuthenticationException e) throws IOException, ServletException {

        // POST请求返回json
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("application/json");

        ErrorResponseData errorResponseData = new ErrorResponseData(
                AuthExceptionEnum.NO_PERMISSION.getCode(), AuthExceptionEnum.NO_PERMISSION.getMessage());

        httpServletResponse.getWriter().write(JSON.toJSONString(errorResponseData));
    }
}
